import { _decorator, Component, Node, tween, Tween, UITransform, v3, Vec3 } from 'cc';
import { Hero } from './Hero';
import { BlockManager } from '../Block/BlockManager';
import { Utils } from '../Utils';
const { ccclass, property } = _decorator;

@ccclass('HeroManager')
export class HeroManager {
    static _instance: HeroManager;
    static get instance() {
        if (this._instance == null) {
            this._instance = new HeroManager();
        }
        return this._instance;
    }
    /** 设置主角坐标 */
    public setHeroAct(pos: Vec3,nodeHero:Node) {
        Tween.stopAllByTarget(nodeHero);
        nodeHero.setScale(1, 1, 1);
        nodeHero.setRotationFromEuler(0, 0, 0);
        nodeHero.getComponent(UITransform).anchorY = 0;
        nodeHero.setPosition(new Vec3(pos.x, pos.y + 200));
        const act_0 = () => {
            nodeHero.getComponent(Hero).canTouch = false; // 禁止触摸
        };
        const actions = [
            { duration: 0.2, position: new Vec3(pos.x, pos.y - 10, 0) },
            { duration: 0.08, position: new Vec3(pos.x, pos.y + 50, 0) },
            { duration: 0.07, position: new Vec3(pos.x, pos.y - 5, 0) },
            { duration: 0.06, position: new Vec3(pos.x, pos.y + 10, 0) },
            { duration: 0.03, position: new Vec3(pos.x, pos.y, 0) },
        ];
        const act_6 = () => {
            nodeHero.getComponent(Hero).canTouch = true; // 恢复触摸
        };
        const tween = new Tween(nodeHero).call(act_0);
        for (const action of actions) {
            tween.to(action.duration, { position: action.position });
        }
        tween.call(act_6).start();
    }

    heroAct_0(nodeHero:Node,heroPos:Vec3,heroHeight:number){
        // 主角移动到初始化位置
        return () => {
            nodeHero.getComponent(UITransform).anchorY = 0.5;
            nodeHero.position = new Vec3(heroPos.x, heroPos.y + heroHeight);
        }
    }

    heroAct_1(nodeHero:Node,posEnd:Vec3){
        return tween(nodeHero).to(0.5, { position: Utils.jumpTo_v3(posEnd, 200) }, { easing: 'sineIn' });
    }

    heroAct_2(nodeHero:Node){
        return tween(nodeHero).to(0.5, { angle: nodeHero.angle + 360 }, { easing: 'sineInOut' });
    }

    heroAct_3(nodeHero:Node){
        return tween(nodeHero).to(0.5, { angle: nodeHero.angle - 360 }, { easing: 'sineInOut' });
    }

    heroAct_4(nodeHero:Node,posEnd:Vec3,heroHeight:number){
        return () => {
            nodeHero.getComponent(UITransform).anchorY = 0;
            nodeHero.position = new Vec3(posEnd.x, posEnd.y - heroHeight);

        }
    }

    heroAct_4_touchEnd(nodeHero:Node,nodeHero2:Node,posEnd:Vec3,heroHeight:number){
        return () => {
            nodeHero.getComponent(UITransform).anchorY = 0
            nodeHero.position = v3(posEnd.x, posEnd.y).subtract(new Vec3(0, heroHeight));
            nodeHero2.active = true;  // 激活主角碰撞体onBeginContact
            setTimeout(() => {
                nodeHero.getComponent(Hero).canTouch = true;
            }, 100);}
    }

    heroAct_touchEnd_over(nodeHero:Node){
        return tween(nodeHero).to(0.5, { position: new Vec3(nodeHero.position.x, nodeHero.position.y - 75, 0) });
    }

}


